/**
 * Copyright (c) 2009  Collaborative Development Group, C.S. Dept., University of Bari
 *
 * All rights reserved. This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0  which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 */
package it.uniba.di.cdg.penelope.ui.lnf;

import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.WorkbenchException;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * Implementation of {@link IEclipseUIHelper}. 
 */
@Singleton
public class EclipseUIHelper implements IEclipseUIHelper {
	
	private final Provider<IWorkbench> workbenchProvider;

	/**
	 * @param workbenchProvider
	 */
	@Inject
	public EclipseUIHelper( Provider<IWorkbench> workbenchProvider ) {
		super();
		this.workbenchProvider = workbenchProvider;
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.penelope.ui.IEclipseUIHelper#switchPerspective(java.lang.String)
	 */
	@Override
	public void switchPerspective( String perspectiveId ) {
		final IWorkbench workbench = workbenchProvider.get();
		try {
			workbench.showPerspective( perspectiveId, workbench.getActiveWorkbenchWindow() );
		} catch (WorkbenchException e) {
			throw new EclipseUIException( e.getMessage() );
		}
//		workbench.getActiveWorkbenchWindow().addPerspectiveListener( listener )
	}
	
	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.penelope.ui.IEclipseUIHelper#closePerspective(java.lang.String)
	 */
	@Override
	public void closePerspective( String perspectiveId ) {
		final IWorkbench workbench = workbenchProvider.get();
		IPerspectiveDescriptor desc = workbench.getPerspectiveRegistry().findPerspectiveWithId( perspectiveId );
		workbench.getActiveWorkbenchWindow().getActivePage().closePerspective( desc, false, false );
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.penelope.ui.IEclipseUIHelper#hideView(java.lang.String)
	 */
	@Override
	public void hideView( String viewId ) {
		final IWorkbench workbench = workbenchProvider.get();
		IViewReference view = workbench.getActiveWorkbenchWindow().getActivePage().findViewReference( viewId );
		workbench.getActiveWorkbenchWindow().getActivePage().hideView( view );
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.penelope.ui.IEclipseUIHelper#showView(java.lang.String)
	 */
	@Override
	public void showView( String viewId ) {
		final IWorkbench workbench = workbenchProvider.get();
		try {
			workbench.getActiveWorkbenchWindow().getActivePage().showView( viewId );
		} catch (PartInitException e) {
			throw new EclipseUIException( e.getMessage() );
		}
	}
}
